<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <el-form ref="saveData" label-width="120px" style="margin-top:30px; margin-left:80px" :model="formData" :rules="rules">
              <!-- 姓名栏 -->
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width:400px" />
              </el-form-item>
              <!-- 密码 -->
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="formData.password2" style="width:400px" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUserDetailById">更新</el-button>
                <el-button type="primary" @click="$router.back()">返回</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- 引入个人详情的userinfo主页 -->
            <userInfo />
          </el-tab-pane>
          <!-- 引入岗位信息的组件 -->
          <el-tab-pane label="岗位信息">
            <jobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
// 引入用户的基本信息的接口
import { getUserDetailById } from '@/api/user'
// 引入保存用户信息的接口
import { saveUserDetailById } from '@/api/employees'
// 引入个人信息组件
import userInfo from '../components/userinfo'
// 引入岗位信息的组件
import jobInfo from '../components/jobinfo'
export default {
  components: {
    userInfo,
    jobInfo
  },

  data() {
    return {
      userId: this.$route.params.id,
      formData: {
        username: '',
        password2: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password2: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 4, max: 10, message: '密码为4-10位数字', trigger: 'blur' }]
      }

    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    async getUserDetailById() {
      this.formData = await getUserDetailById(this.userId)
      console.log(this.formData)
    },
    async saveUserDetailById() {
      try {
        await this.$refs.saveData.validate()
        await saveUserDetailById({ ...this.formData, password: this.formData.password2 })
        this.$message.success('修改成功')
        this.$router.back()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
